package cn.tedu.controller;

import cn.tedu.pojo.Product;
import cn.tedu.pojo.Types;
import cn.tedu.service.ProductService;
import cn.tedu.service.TypesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    TypesService typesService;

    //分页功能
    @RequestMapping("/productList")
    public String productList(
            @RequestParam(value = "name",defaultValue = "") String name,
            @RequestParam(value = "isHot",defaultValue = "2") Integer isHot,
            @RequestParam(value = "typeId",defaultValue = "-1") Integer typeId,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            Model model){
        //设置pageSize的值(每页显示的数据记录数)
        int pageSize = 3;

        //查询product数据
        //List <Product> products = productService.selectProductByMH(name, isHot, typeId);
        //查询Types数据
        List <Types> types = typesService.findAll();

        //获得数据的条数(总数)
        int num = productService.selectProductByMH(name, isHot, typeId).size();
        //总页数
        int total = num%pageSize == 0? num/pageSize : num/pageSize+1;
        //分页的方法
        List <Product> products = productService.selectProductByLimit(name, isHot, typeId, pageNum, pageSize);

        //model对象存数据
        model.addAttribute("products",products);
        model.addAttribute("types",types);
        model.addAttribute("name",name);
        model.addAttribute("isHot",isHot);
        model.addAttribute("typeId",typeId);

        model.addAttribute("pageNum",pageNum);
        model.addAttribute("total",total);

        return "product_list";
    }

    //基于mybatis分页插件
    @RequestMapping("/productListPageHelper")
    public String productListPageHelper(
            @RequestParam(value = "name",defaultValue = "") String name,
            @RequestParam(value = "isHot",defaultValue = "2") Integer isHot,
            @RequestParam(value = "typeId",defaultValue = "-1") Integer typeId,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            Model model
    ){
        //每页显示记录数
        int pageSize = 3;
        //分页插件
        PageHelper.startPage(pageNum,pageSize);
        //查询product集合数据
        List <Product> products = productService.selectProductByMH(name, isHot, typeId);

        //使用pageInfo对象构造product集合数据
        PageInfo<Product> pageInfo = new PageInfo(products);

        //查询types集合数据
        List <Types> types = typesService.findAll();


        //使用model对象存数据
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",types);

        model.addAttribute("name",name);
        model.addAttribute("isHot",isHot);
        model.addAttribute("typeId",typeId);
        model.addAttribute("pageNum",pageNum);

        return "product_list_pageHelper";
    }

    //删除功能
    @RequestMapping("/delete")
    public String delete(Integer id){
        //调用业务层删除方法
        productService.deleteById(id);

        return "redirect:/productListPageHelper.do";
    }

    //跳转到add.jsp页面
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        //查询types数据
        List <Types> types = typesService.findAll();
        model.addAttribute("types",types);
        return "add";
    }

    //执行添加功能
    @RequestMapping("/add")
    public String add(Product product){
        System.out.println("product------------"+product);
        productService.add(product);
        //重定向
        return "redirect:/productListPageHelper.do";
    }
}
